<template>
  <div class="wrapper">
    <div class="uc-main clearfix">
      <div class="uc-aside">
        <user-menu></user-menu>
      </div>
      <div class="uc-content">
        <div class="uc-panel">
          <div class="uc-bigtit">商品列表</div>
          <div class="uc-panel-bd">
            <el-table
              :data="data"
              style="width: 100%">
              <el-table-column
                label="商品Id"
                prop="commodityId">
              </el-table-column>
              <el-table-column
                label="商品名称"
                prop="commodityName">
              </el-table-column>
              <el-table-column
                label="图片"
                prop="date">
                <template slot-scope="scope">
                  <img  :src="scope.row.commodityImgurl" alt="" style="width: 100%;height: 100%">
                </template>
              </el-table-column>
              <el-table-column
                label="商品单价"
                prop="commodityPrice">
              </el-table-column>
              <el-table-column
                label="商品库存"
                prop="commodityNum">
              </el-table-column>
              <el-table-column
                align="right">
                <template slot="header" slot-scope="scope">
                  <el-input
                    v-model="search"
                    size="mini"
                    placeholder="输入名字搜索"/>
                  <el-button
                    size="mini"
                    @click="queryStart()">查询</el-button>
                  <el-button
                    size="mini"
                    @click="openFrom()">新增</el-button>
                </template>
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @current-change="handleCurrentChange"
              :page-size="pageSize"
              :pager-count="5"
              layout="prev, pager, next"
              :total="total">
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import UserMenu from './UserMenu'
import Util from '../../../common/util'
import http from '../../../base/api/http'
export default {
  name: 'AdminCategoryWrapper',
  components: {
    UserMenu
  },
  data () {
    return {
      pageSize: 10,
      form: {
        categoryId: '',
        categoryName: ''
      },
      tableData: [],
      data: [],
      search: null,
      myHeaders: {
        Authorization: Util.getToken()
      },
      formLabelWidth: '120px',
      query: {
        commodityName: null,
        brandId: null,
        categoryId: null
      }
    }
  },
  computed: {
    total () {
      return this.tableData.length
    }
  },
  methods: {
    handleCurrentChange (val) {
      this.data = this.tableData.slice((val - 1) * this.pageSize, val * this.pageSize)
    },
    handleEdit (index, row) {
      this.$router.push({path: '/commodityDetails', query: { commodityId: row.commodityId }})
    },
    handleDelete (index, row) {
      this.open2(row.categoryId)
    },
    openFrom () {
      this.$router.push('/commodityDetails')
    },
    deleteCategoryId (id) {
      http.requestGet('/java/api/category/deleteCategory', {
        'categoryId': id
      }).then((res) => {
        if (res.status === 0) {
          alert('删除成功')
          this.$router.go(0)
        } else {
          alert('删除失败')
        }
      })
    },
    open2 (id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteCategoryId(id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    queryStart () {
      this.query.commodityName = this.search
      this.findAll()
    },
    findAll () {
      http.requestGet('/java/api/commodity/findCommodityList', {
        'commodityName': this.query.commodityName,
        'brandId': this.query.brandId,
        'categoryId': this.query.categoryId
      }).then((res) => {
        if (res.status === 0) {
          this.tableData = res.data
          this.handleCurrentChange(1)
        }
      })
    }},
  mounted () {
    this.findAll()
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 100%;
    height: 100%;
    display: block;
  }
</style>
